package net.bolue.sysmng.console;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.bolue.sysmng.db.DBMetaManager;
import net.bolue.sysmng.db.DbOpUtil;
import net.bolue.sysmng.db.metadata.ColMetaData;
import net.bolue.sysmng.db.metadata.PKeyMetaData;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.model.ColInfoModel;
import net.bolue.sysmng.model.TblInfoModel;
import net.bolue.sysmng.tblbean.ColInfo;
import net.bolue.sysmng.tblbean.TblInfo;
import net.bolue.sysmng.util.DbMetaUtil;
import net.bolue.web.report.RepConstants;

public class ColInfoAdmin {

	public void insertBatchTblInfos(String subSys, String[] selTbls) throws BolueException{
		for (int i = 0; i < selTbls.length; i++) {
			insertTblInfo(subSys,selTbls[i]);
		}
	}
	
	public int insertTblInfo(String subSys, String selTbl) throws BolueException{
		String tableName = selTbl.substring(selTbl.lastIndexOf(".") + 1);
		TblInfoModel tblInfoModel = new TblInfoModel();
		TblInfo tblInfo = tblInfoModel.getTblInfo(subSys, selTbl);
		if(tblInfo==null){
			tblInfo = new TblInfo();
			tblInfo.setSubSys(subSys);
			tblInfo.setTblName(tableName);
		}
		List<ColInfo> colInfoList = getColInfo(subSys,selTbl);
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY,true);
		Map<String,Object> whereMap = new HashMap<String,Object>();
		whereMap.put("SUBSYS", subSys);
		whereMap.put("TBLNAME", selTbl);
		dbOpUtil.delete("st_tblinfo", whereMap);
		int i = dbOpUtil.insert(tblInfo);
		dbOpUtil.delete("st_colinfo", whereMap);
		for(ColInfo colInfo : colInfoList){
			dbOpUtil.insert(colInfo);
		}
		dbOpUtil.commitTrans();
		return i;
	}
	
	public void repairColInfos(String subSys, String selTbl)throws BolueException{
		List<ColInfo> colInfoList = getColInfo(subSys,selTbl);
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY,true);
		Map<String,Object> whereMap = new HashMap<String,Object>();
		whereMap.put("SUBSYS", subSys);
		whereMap.put("TBLNAME", selTbl);
		dbOpUtil.delete("st_colinfo", whereMap);
		for(ColInfo colInfo : colInfoList){
			dbOpUtil.insert(colInfo);
		}
		dbOpUtil.commitTrans();
	}
	
	public List<ColInfo> getColInfo(String subSys, String selTbl) throws BolueException{
		List<ColInfo> colInfoList = getColInfoByMeta(subSys,selTbl);
		ColInfoModel colInfoModel = new ColInfoModel();
		Map<String,ColInfo> colInfoMap = colInfoModel.getColInfoMap(subSys, selTbl);
		for(int i=0;i<colInfoList.size();i++){
			ColInfo colInfo = colInfoList.get(i);
			String colName = colInfo.getColName();
			ColInfo localColInfo = colInfoMap.get(colName);
			 if (localColInfo != null){
				 colInfo.setColDesc(localColInfo.getColDesc());
				 colInfo.setDicKind(localColInfo.getDicKind());
				 colInfo.setDicSubSys(localColInfo.getDicSubSys());
				 colInfo.setDicGrpid(localColInfo.getDicGrpid());
				 colInfo.setDispLen(localColInfo.getDispLen());
				 colInfo.setTagType(localColInfo.getTagType());
				 colInfo.setPostFixTag(localColInfo.getPostFixTag());
			 }
		}
		return colInfoList;
	}
	
	public List<ColInfo> getColInfoByMeta(String subSys, String selTbl) throws BolueException{
		List<ColInfo> colInfoList = new ArrayList<ColInfo>();
		List<ColMetaData> colMetaList = DBMetaManager.getAllColumns(subSys, selTbl);
		Map<String,PKeyMetaData> pkMap = DBMetaManager.getPKeyColumnsMap(subSys, selTbl);
		if(colMetaList!=null&&colMetaList.size()>0){
			String tableName = selTbl.substring(selTbl.lastIndexOf(".") + 1);
			for (int j = 0; j < colMetaList.size(); j++){
				ColMetaData colMetaData = colMetaList.get(j);
				String columnName = colMetaData.getColumnName();
				ColInfo colInfo = new ColInfo();
				colInfo.setSubSys(subSys);
				colInfo.setTblName(tableName);
				colInfo.setColName(columnName);
				colInfo.setDispNo(j + 1);
				String colDesc = columnName.toUpperCase();
				colInfo.setColDesc(colDesc);
				Integer jType = DbMetaUtil.parseJType(colMetaData.getDataType(),colMetaData.getColumnSize());
				colInfo.setJType(jType);
				Integer isPkey = 0;
				if(pkMap!=null&&pkMap.get(columnName)!=null){
					isPkey = 1;
				}
				colInfo.setIsPkey(isPkey);
				if (colMetaData.getNullable() == 0){
					colInfo.setIsNullAble(0);
				}else{
					colInfo.setIsNullAble(1);
				}
				colInfo.setDicKind(0);
				if(jType==4){
					colInfo.setColSize(19);
				}else{
					colInfo.setColSize(colMetaData.getColumnSize());
				}
				Integer dispLen = 0;
				if ((jType == 5) || (jType == 1) || (jType == 2)){
					dispLen = 20;
				}else if ((jType < 50) && (jType > 0)){
					dispLen = 20;
				}else{
					dispLen = 50;
				}
				colInfo.setDispLen(dispLen);
				colInfo.setTagType(1);
				colInfo.setDecDigits(0);
				colInfoList.add(colInfo);
			}
		}
		return colInfoList;
	}
	
}
